<template>
  <van-popup position="center" :show="value" custom-style="width:90%;border-radius: 12rpx;" @close="close">
    <image :src="imageUrl(adInfo.image)" class="bannerImg" @tap="toPage" @longpress="saveCode(adInfo.image)"></image>
  </van-popup>
</template>
<script>
import homeApi from "@/api/home";

export default {
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    position: {
      type: String || Number,
    },
    params: {
      type: Object,
      default: { inviteUserId: "" },
    },
  },
  data() {
    return {
      adInfo: {},
    };
  },
  computed: {
    imageUrl() {
      return url => {
        return this.config_url.imageUrl + url;
      };
    },
  },
  mounted() {
    this.bannerContent();
  },
  methods: {
    // ----------------- 长按保存图片 ---------------------
    saveCode(codeUrl) {
      const _self = this;
      uni.showModal({
        title: "提示",
        content: "确定保存到相册吗",
        success(res) {
          if (res.confirm) {
            _self.downloadFile(codeUrl);
          }
        },
      });
    },
    downloadFile(codeUrl) {
      const _self = this;
      uni.downloadFile({
        url: this.config_url.imageUrl + codeUrl, // 图片地址
        success: res => {
          if (res.statusCode === 200) {
            uni.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success() {
                uni.showToast({
                  title: "保存成功",
                  icon: "none",
                });
              },
              fail(e) {
                _self.getSetting();
              },
            });
          }
        },
      });
    },
    toPage() {
      if (!this.adInfo.url) return;
      this.fGoTo(`${this.adInfo.url}?inviteUserId=${this.params.inviteUserId}`);
    },
    async bannerContent() {
      const { data } = await homeApi.bannerList({
        position: this.position,
        platform: 2,
      });
      if (data && data.length > 0) {
        this.adInfo = data[0];
      }
    },
    close() {
      this.$emit("input", false);
    },
  },
};
</script>
<style lang="scss" scoped>
.bannerImg {
  width: 100%;
  height: 93vw;
}
</style>
